package wechat

import (
	"encoding/json"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"
)

type TemplateHomeworkNoticeDataValue struct {
	Value string `json:"value"`
}

type TemplateHomeworkNoticeData struct {
	First    TemplateHomeworkNoticeDataValue `json:"first"`
	Keyword1 TemplateHomeworkNoticeDataValue `json:"keyword1"` // 姓名
	Keyword2 TemplateHomeworkNoticeDataValue `json:"keyword2"` // 时间
	Keyword3 TemplateHomeworkNoticeDataValue `json:"keyword3"` // 已完成作业
	Keyword4 TemplateHomeworkNoticeDataValue `json:"keyword4"` // 未完成作业
	Remark   TemplateHomeworkNoticeDataValue `json:"remark"`
}

type TemplateHomeworkNotice struct {
	Touser     string                     `json:"touser"`
	TemplateId string                     `json:"template_id"`
	Url        string                     `json:"url"`
	Data       TemplateHomeworkNoticeData `json:"data"`
}

type TemplateHomeworkNoticeRes struct {
	Errcode int    `json:"errcode"`
	Errmsg  string `json:"errmsg"`
	Msgid   int64  `json:"msgid"`
}

// 发送作业通知
func SendHomeworkNotice(openid, first, remark, url, k1, k2, k3, k4 string) {
	t := TemplateHomeworkNotice{
		Touser:     openid,
		TemplateId: beego.AppConfig.String("homeworknoticeid"),
		Url:        url,
		Data: TemplateHomeworkNoticeData{
			First:    TemplateHomeworkNoticeDataValue{Value: first},
			Keyword1: TemplateHomeworkNoticeDataValue{Value: k1},
			Keyword2: TemplateHomeworkNoticeDataValue{Value: k2},
			Keyword3: TemplateHomeworkNoticeDataValue{Value: k3},
			Keyword4: TemplateHomeworkNoticeDataValue{Value: k4},
			Remark:   TemplateHomeworkNoticeDataValue{Value: remark},
		},
	}
	content, err := json.Marshal(t)
	if err != nil {
		logs.Error("can not marshal homework notice struct:", err)
		return
	}
	ret := SendTemplateMsg("anban", string(content))
	res := TemplateHomeworkNoticeRes{}
	err = json.Unmarshal([]byte(ret), &res)
	if err != nil {
		logs.Error("can not unmarshal send homework notice response:", err)
		return
	}
	if res.Errcode != 0 {
		logs.Error("send homework notice fail:", res.Errmsg)
		return
	}
}
